//
//  ZMSortViewController.m
//  DaMei
//
//  Created by mac on 16/2/24.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ZMSortViewController.h"
#import "ZMSortModel.h"
#import "ZMDropdownView.h"
#import "ZMCategoryModel.h"

@interface ZMSortViewController ()

@property (nonatomic, strong) NSArray *sortArray;

@end

@implementation ZMSortViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        //1. 加载排序的模型数据
        self.sortArray = [ZMSortModel objectArrayWithFilename:@"sorts.plist"];
        
        //2. 循环创建里面7个按钮
        NSInteger count = self.sortArray.count;
        
        // frame
        CGFloat width = 100;
        CGFloat height = 30;
        CGFloat margin = 15;
        
        // 创建按钮
        for (int i = 0; i < count; i++) {
            // 初始化
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            
            // 取出sort模型数据
            ZMSortModel *sortModel = self.sortArray[i];
            
            // 设置标题
            [button setTitle:sortModel.label forState:UIControlStateNormal];
            
            // 设置文字颜色
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
            
            // 设置背景图片
            [button setBackgroundImage:[UIImage imageNamed:@"btn_filter_normal"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"btn_filter_selected"] forState:UIControlStateHighlighted];
            
            // 设置frame
            button.width = width;
            button.height = height;
            button.x = margin;
            button.y = margin + (button.height + margin) * i;
            
            // 绑定tag --> 区分点击了哪一个按钮
            button.tag = i;
            
            // 添加方法
            [button addTarget:self action:@selector(sortButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.view addSubview:button];
        }
        
        //3. 设置popover的大小
        CGFloat popoverW = margin * 2 + width;
        CGFloat popoverH = margin + (height + margin) * count;
        self.preferredContentSize = CGSizeMake(popoverW, popoverH);
        

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
   }

#pragma mark 排序按钮点击
- (void)sortButtonClick:(UIButton *)button {
    
    // 发送通知
    [ZMNotificationCenter postNotificationName:ZMSortDidChangeNotifacation object:nil userInfo:@{ZMSelectSortModel : self.sortArray[button.tag]}];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
